package com.heima.Read;

import com.heima.bean.Manager;
import com.heima.bean.Subway;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class ManagerRead {
    public static void main(String[] args) throws IOException {
        ArrayList<Manager> list = new ArrayList<>();
        BufferedReader bf = new BufferedReader(new FileReader("day15\\manager.txt"));
        String line;
        while ((line = bf.readLine()) != null) {
            String[] split = line.split(",");
            String id = split[0];
            int i = Integer.parseInt(id);
            String name = split[1];
            String sex = split[2];
            String phone = split[3];
            Manager manager = new Manager(i, name, sex, phone);
            list.add(manager);
        }
//        for (Manager manager1 : list) {
//            System.out.println(manager1);
//        }
        bf.close();


        ArrayList<Subway> list1 = new ArrayList<>();
        BufferedReader bu = new BufferedReader(new FileReader("day15\\subway.txt"));
        String len;
        while ((len = bu.readLine()) != null) {
            String[] split = len.split(",");
            String id = split[0];
            String name = split[0];
            String line1 = split[0];
            Subway subway = new Subway(id, name, line1);
            list1.add(subway);
        }
        bu.close();

        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入管理员id：");
            String managerid = sc.next();
            System.out.println("请输入管理员姓名：");
            String managername = sc.next();
            boolean flag = getId(managerid, managername, list);
            if (flag) {
                System.out.println("登录成功");
                success(list1);
            } else {
                System.out.println("登录失败");
            }
        }
    }

    private static void success(ArrayList<Subway> list1) throws IOException {
        while (true) {
            System.out.println("请输入选择： 1.查看地铁站  2.添加地铁站");
            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            switch (choose) {
                case "1":
                    findsubway(list1);
                    break;
                case "2":
                    addsubway(list1);
                    break;
            }
        }
    }

    private static void addsubway(ArrayList<Subway> list1) throws IOException {
//        5.1.依次给出提示,获取管理人员输入的地铁站信息,包括地铁站编号，地铁站名称，所属路线
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入地铁Id");
        String id = sc.next();
        System.out.println("请输入地铁名称");
        String name = sc.next();
        System.out.println("请输入地铁路线");
        String line = sc.next();
//        5.2. 将管理人员输入的地铁站信息封装成对象,存储到集合中
        Subway subway=new Subway(id,name,line);
        list1.add(subway);
//        5.3. 将集合中的地铁站信息更新到文件中,再次打开文件时,应该包含管理人员刚才录入进去的地铁站信息,程序结束
        BufferedWriter buu=new BufferedWriter(new  FileWriter("day15\\subway.txt"));
        for (Subway subway1 : list1) {
           buu.write(subway1.getSubwayid()+subway1.getSubname()+subway1.getSubline()+"\r\n");
        }
        buu.close();
    }

    private static void findsubway(ArrayList<Subway> list1) {
//        4.1. 判断地铁站集合中是否有地铁站信息
        if(list1!=null){
            for (Subway subway : list1) {
                System.out.println(subway);
            }
        }else {
            System.out.println("无信息");
        }
//        4.2. 如果没有地铁站信息,给出提示
//        4.3. 如果有地铁站信息,遍历集合,打印每个地铁站信息
    }

    private static boolean getId(String managerid, String managername, ArrayList<Manager> list) {
        boolean result = false;
        for (Manager manager : list) {
            if (managerid.equals(manager.getManagerid()) || managername.equals(manager.getName())) {
                result = true;
            }
        }
        return result;

    }
}
